package com.gurun.camera.device.model.impl;

import com.danale.sdk.platform.entity.device.Device;
import com.gurun.camera.device.bean.MultiDevice;
import com.gurun.camera.device.bean.SingleDevice;
import com.gurun.camera.device.bean.VideoDevice;
import com.gurun.camera.device.constant.VideoDataType;
import com.gurun.camera.device.model.IVideoPlayModel;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import rx.Observable;
import rx.Observer;

public class VideoPlayerModel implements IVideoPlayModel {
    LinkedHashMap<String, VideoDevice> mDeviceLinkedHashMap;
    LinkedList<VideoDevice> mDevices;
    int mCurSelectScreen;
    VideoDataType videoDataType;

    VideoPlayerModel(VideoDataType type){
        this.videoDataType = type;
        mDevices = new LinkedList<>();
        mDeviceLinkedHashMap = new LinkedHashMap<>();
    }

    @Override
    public LinkedList<VideoDevice> getDevices() {
        return mDevices;
    }

    @Override
    public LinkedHashMap<String, VideoDevice> getDeviceMap() {
        return mDeviceLinkedHashMap;
    }

    @Override
    public void setDevices(List<Device> devices) {
        Observable.from(devices).subscribe(new Observer<Device>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Device device) {
                switch (videoDataType){
                    case ONLINE_NVR:
                        MultiDevice multiDevice = new MultiDevice.Builder()
                                .setDevice_id(device.getDeviceId())
                                .setDeviceType(device.getProductTypes())
                                .setChanNum(device.getChannelNum())
                                .setOnlineType(device.getOnlineType())
                                .build();
                        mDevices.addLast(multiDevice);
                        mDeviceLinkedHashMap.put(multiDevice.getDevice_id(), multiDevice);
                        break;
                    case DOORBELL:
                    case GARAGE:
                    case IPC_HUB:
                    case ONLINE_IPC:
                        SingleDevice singleDevice = new SingleDevice.Builder()
                                .setDevice_id(device.getDeviceId())
                                .setDeviceType(device.getProductTypes())
                                .setOnlineType(device.getOnlineType())
                                .setCur_chan(device.getChannelNum())
                                .build();
                        mDevices.addLast(singleDevice);
                        mDeviceLinkedHashMap.put(singleDevice.getDevice_id(), singleDevice);
                        break;
                    case RECORD:
                    case CLOUD_SINGLE:
                    case DISK:
                    case CLOUD:
                        SingleDevice singDevice = new SingleDevice.Builder()
                                .setDevice_id(device.getDeviceId())
                                .setDeviceType(device.getProductTypes())
                                .setOnlineType(device.getOnlineType())
                                .setCur_chan(device.getChannelNum())
                                .build();
                        mDevices.addLast(singDevice);
                        mDeviceLinkedHashMap.put(singDevice.getDevice_id(), singDevice);
                        break;
                }
            }
        });
    }

    @Override
    public void setCurSelectScreen(int mCurSelectScreen) {
        this.mCurSelectScreen = mCurSelectScreen;
    }

    @Override
    public VideoDataType getDataType() {
        return videoDataType;
    }
}
